/*
 * Copyright (C) 2008 Shane Isbell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.atom.parser;

import org.kxml2.io.KXmlParser;
import org.slideme.atom.parser.entity.AtomEntityContext;
import org.slideme.atom.parser.entity.TextEntityHandler;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

public class AtomReader {

    private AtomEntityContext ctx;

    public AtomReader() {
        ctx = new AtomEntityContext();
        ctx.addAtomEntityHandler(new TextEntityHandler());
    }

    public void setContext(AtomEntityContext ctx) {
        this.ctx = ctx;
    }

    public Feed readFeedFrom(URI serverUri) {
        HttpURLConnection connection = null;
        InputStream is = null;
        try {
            URL url = serverUri.toURL();
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            is = connection.getInputStream();
            return Feed.marshalFrom(ParserUtils.createParserFrom(is), ctx);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        finally {
            if (is != null) {
                try {
                    System.out.println("Message = " + connection.getResponseMessage());
                    is.close();
                } catch (IOException e) {

                }
            }
        }

        return null;
    }

    public Entry readEntryFrom(URI serverUri) {
        HttpURLConnection connection = null;
        InputStream is = null;
        try {
            URL url = serverUri.toURL();
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            is = connection.getInputStream();
            KXmlParser parser = ParserUtils.createParserFrom(is);
            parser.nextTag();
            return Entry.marshalFrom(parser, ctx);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        finally {

            if (is != null) {
                try {
                    System.out.println("Message = " + connection.getResponseMessage());
                    is.close();
                } catch (IOException e) {

                }
            }
        }

        return null;
    }
}
